package c_synchronized;

public class Ticket2 implements Runnable {
    private int ticket = 100;

    private Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            sellticket();
        }

    }

    private synchronized void sellticket() {
        if (ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Thread t = Thread.currentThread();
            String name = t.getName();
            System.out.println(name + "正在卖票:" + ticket);
            ticket--;
        }
    }
}
